package com.imooc.miaosha.controller;

import com.imooc.miaosha.domain.MiaoshaUser;
import com.imooc.miaosha.result.Result;
import com.imooc.miaosha.service.MiaoshaUserService;
import com.imooc.miaosha.vo.LoginVo;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @author liyuhui
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {
    private static Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    MiaoshaUserService miaoshaUserService;

    @RequestMapping("/")
    @ResponseBody
    public String home() {
        return "Hello Goods!";
    }

    /**
     *
     * @param model
//     * @param cookieToken token存放于cookie中，非必须有的参数
//     * @param paramToken token存放于请求的参数中，非必须有的参数
       * @param miaoshaUser
     * @return
     */
    @RequestMapping("/to_list")
    public String list(
//            HttpServletResponse response,
                          Model model,
//                          @CookieValue(value = MiaoshaUserService.COOKI_NAME_TOKEN,required = false) String cookieToken,
//                          @RequestParam(value = MiaoshaUserService.COOKI_NAME_TOKEN,required = false) String paramToken,
                            MiaoshaUser miaoshaUser) {
//        //优先从请求参数中获取token，如果都没有则返回登录页
//        if (StringUtils.isEmpty(cookieToken) && StringUtils.isEmpty(paramToken)){
//            return "login";
//        }
//
//        String token = StringUtils.isEmpty(paramToken)? cookieToken:paramToken;
//        MiaoshaUser miaoshaUser = miaoshaUserService.getByToken(response,token);

        model.addAttribute("user",miaoshaUser);
        return "goods_list";
    }

}
